package org.xtframe.util;

import java.util.List;
import java.util.Map;

import org.xtframe.dao.DataBaseDao;

public class WorkflowUtil {

	/**
	 * 获取任务页面配置参数
	 * 
	 * @param taskId
	 * @param formValue
	 * @return
	 */
	public static final String TASK_PAGE_CACHE_KEY_PREFIX = "taskPage_";// TaskPage缓存Key前缀

	public static Map<String, Object> getTaskPage(String processDefinitionId, String formValue) {
		String key = TASK_PAGE_CACHE_KEY_PREFIX + formValue;

		@SuppressWarnings("unchecked")
		List<Map<String, Object>> result = (List<Map<String, Object>>) OsCacheUtil.getFromCache(key);
		if (result == null) {
			String strSql = CommonUtil.createSql(SystemConfigUtil.getSystemConfig().getTaskPageSql(), formValue);
			DataBaseDao dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
			result = dataBaseDao.executeQuery(strSql, null);
		}

		if (result == null || result.size() <= 0)
			return null;
		OsCacheUtil.putInCache(key, result);
		if (processDefinitionId == null)
			return result.get(0);

		Map<String, Object> taskPage = null;
		for (Map<String, Object> page : result) {
			if (processDefinitionId.equals(page.get("processdefinitionid"))) {
				taskPage = page;
				break;
			}
		}
		if (taskPage == null)
			taskPage = result.get(0);

		return taskPage;
	}

	/**
	 * 保存任务记录
	 * 
	 * @param recordId
	 * @param processInstanceId
	 * @param taskId
	 * @param userId
	 * @param subject
	 * @param opinion
	 * @param outcome
	 * @param time
	 * @param pageId
	 * @return
	 */
	public static boolean saveTaskRecord(String recordId, String processInstanceId, String taskId, String userId, String subject,
			String opinion, String outcome, String time, String pageId) {
		String strSql = CommonUtil.createSql(SystemConfigUtil.getSystemConfig().getTaskRecordSql(), recordId, processInstanceId,
				taskId, userId, pageId, subject, opinion, outcome, time);
		DataBaseDao dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		return dataBaseDao.executeUpdate(strSql, null) == 1;
	}

	/**
	 * 删除任务记录
	 * 
	 * @param recordId
	 */
	public static void deleteTaskRecord(String recordId) {
		String strSql = CommonUtil.createSql(SystemConfigUtil.getSystemConfig().getDeleteTaskRecordSql(), recordId);
		DataBaseDao dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		dataBaseDao.executeUpdate(strSql, null);
	}

	/**
	 * 设置开启流程
	 * 
	 * @param processInstanceId
	 * @param processName
	 * @param userId
	 * @param startTime
	 * @return
	 */
	public static boolean startProcess(String processInstanceId, String processName, String userId, String startTime) {
		String strSql = CommonUtil.createSql(SystemConfigUtil.getSystemConfig().getStartProcessSql(), processInstanceId,
				processName, userId, startTime);
		DataBaseDao dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		return dataBaseDao.executeUpdate(strSql, null) == 1;
	}

	/**
	 * 设置关闭流程
	 * 
	 * @param processInstanceId
	 * @param time
	 */
	public static void stopProcess(String processInstanceId, String time) {
		String strSql = CommonUtil.createSql(SystemConfigUtil.getSystemConfig().getStopProcessSql(), processInstanceId, time);
		DataBaseDao dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		dataBaseDao.executeUpdate(strSql, null);
	}
}
